package org.jeecg.modules.yc.NB.Services;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctg.ag.sdk.biz.AepDeviceCommandClient;
import com.ctg.ag.sdk.biz.AepDeviceManagementClient;
import com.ctg.ag.sdk.biz.aep_device_command.*;
import com.ctg.ag.sdk.biz.aep_device_management.*;
import org.jeecg.modules.yc.NB.Model.Device;
import org.jeecg.modules.yc.NB.Model.NBResult;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class NBCommandService {

    public final static String masterKey = "0dce92073c4d47ab92f37a300e9456b1";
    public final static int productId = 17060459;


    private AepDeviceCommandClient buildAepDeviceEventClient() {
        AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(
                "Tyn9lldaBf3").appSecret("zf7wlyuikC").build();
        return client;
    }

    private AepDeviceManagementClient buildAepDeviceManagementClient() {
        AepDeviceManagementClient client = AepDeviceManagementClient.newClient()
                .appKey("Tyn9lldaBf3").appSecret("zf7wlyuikC")
                .build();
        return client;
    }

    /**
     * 设备创建
     *
     * @param MasterKey
     * @param jsonBody
     * @return
     * @throws Exception
     */
    public CreateDeviceResponse CreateDevice(String MasterKey, byte[] jsonBody) throws Exception {
        AepDeviceManagementClient client = buildAepDeviceManagementClient();
        CreateDeviceRequest request = new CreateDeviceRequest();
        request.setParamMasterKey(MasterKey);
        request.setBody(jsonBody);
        CreateDeviceResponse createDeviceResponse = client.CreateDevice(request);
        client.shutdown();
        return createDeviceResponse;
    }
    /**
     * 设备跟新
     *
     * @param MasterKey
     * @param jsonBody
     * @return
     * @throws Exception
     */
    public UpdateDeviceResponse UpdateDevice (String MasterKey, byte[] jsonBody) throws Exception {
        AepDeviceManagementClient client = buildAepDeviceManagementClient();
        UpdateDeviceRequest request = new UpdateDeviceRequest();
        request.setParamMasterKey(MasterKey);
        request.setBody(jsonBody);
        UpdateDeviceResponse updateDeviceResponse = client.UpdateDevice(request);
        client.shutdown();
        return updateDeviceResponse;
    }
    /**
     * 设备查询
     * @param deviceId
     * @return
     * @throws Exception
     */
    public Device queryDevice(String deviceId) throws Exception {
        AepDeviceManagementClient client = buildAepDeviceManagementClient();
        QueryDeviceRequest request = new QueryDeviceRequest();
        request.setParamMasterKey(masterKey);
        request.setParamDeviceId(deviceId);	// single value
        request.addParamProductId(productId);	// or multi values
        QueryDeviceResponse queryDeviceResponse =  client.QueryDevice(request);
        client.shutdown();
        if(queryDeviceResponse.getStatusCode() == 200){
            NBResult nbResult = JSON.parseObject(new String(queryDeviceResponse.getBody(), StandardCharsets.UTF_8), NBResult.class);
            return JSON.parseObject(nbResult.getResult(),Device.class);
        }
        return null;
    }
    /**
     * 设备查询
     * @param deviceIds
     * @return
     * @throws Exception
     */
    public DeleteDeviceResponse deleteDevice(String deviceIds) throws Exception {
        AepDeviceManagementClient client = buildAepDeviceManagementClient();
        DeleteDeviceRequest request = new DeleteDeviceRequest();
        request.setParamMasterKey(masterKey);
        request.setParamDeviceIds(deviceIds);	// single value
        request.addParamProductId(productId);	// or multi values
        DeleteDeviceResponse deleteDeviceResponse =  client.DeleteDevice(request);
        client.shutdown();
        return deleteDeviceResponse;
    }

    /**
     * 取消指令
     *
     * @param MasterKey
     * @param jsonBody
     * @return
     */
    public Object CancelCommand(String MasterKey, byte[] jsonBody) throws Exception {
        CancelCommandRequest request = new CancelCommandRequest();
        request.setParamMasterKey(MasterKey);
        request.setBody(jsonBody);
        AepDeviceCommandClient client = buildAepDeviceEventClient();
        CancelCommandResponse response = client.CancelCommand(request);
        Object object = null;
        return null;
    }

    /**
     * 统一合并指令下发
     *
     * @param MasterKey
     * @param jsonBody
     * @return
     */
    public void  CreateCommand(String MasterKey, byte[] jsonBody) throws Exception {
        CreateCommandRequest request = new CreateCommandRequest();
        request.setParamMasterKey(MasterKey);
        request.setBody(jsonBody);
        AepDeviceCommandClient client = buildAepDeviceEventClient();
        CreateCommandResponse response = client.CreateCommand(request);
    }

    /**
     * 查询单个指令详情
     *
     * @param MasterKey
     * @param commandId
     * @param productId
     * @param deviceId
     * @return
     */
    public Object QueryCommand(String MasterKey, String commandId, int productId, String deviceId) throws Exception {
        QueryCommandRequest request = new QueryCommandRequest();
        request.setParamCommandId(commandId);
        request.setParamDeviceId(deviceId);
        request.setParamMasterKey(MasterKey);
        request.setParamProductId(productId);
        AepDeviceCommandClient client = buildAepDeviceEventClient();
        QueryCommandResponse response = client.QueryCommand(request);
        return null;
    }

    /**
     * 批量查询指令详情
     *
     * @param MasterKey
     * @param productId
     * @param deviceId
     * @param startTime
     * @param endTime
     * @param pageNow
     * @param pageSize
     * @return
     */
    public Object QueryCommandList(String MasterKey, int productId, String deviceId, String startTime, String endTime,
                                   int pageNow, int pageSize) throws Exception {
        QueryCommandListRequest request = new QueryCommandListRequest();
        request.setParamDeviceId(deviceId);
        request.setParamMasterKey(MasterKey);
        request.setParamEndTime(endTime);
        request.setParamStartTime(startTime);
        request.setParamProductId(productId);
        request.setParamPageNow(pageNow);
        request.setParamPageSize(pageSize);
        AepDeviceCommandClient client = buildAepDeviceEventClient();
        QueryCommandListResponse response = client.QueryCommandList(request);
        return null;
    }

    public static void main(String[] args) throws Exception {
        JSONObject other = new JSONObject();
        other.put("autoObserver",0);
        JSONObject body = new JSONObject();
        body.put("deviceName","测试主站创建设备");
        body.put("deviceSn","66668888");
        body.put("imei","152345687412451");
        body.put("operator","主站操作员");
        body.put("other",other);
        body.put("productId",productId);
        CreateDeviceResponse createDeviceResponse = new NBCommandService().CreateDevice(masterKey, body.toJSONString().getBytes(StandardCharsets.UTF_8));
        System.out.println(JSON.parseObject(new String(createDeviceResponse.getBody(), StandardCharsets.UTF_8)));
        System.err.println(createDeviceResponse);
    }
}